function [net, TrainingCmd] = genTrainingCmd(net, solv, cmd, Train, Test, flag)

if nargin < 6
    flag = '';
end

cmd = mycaffe.remove_empty_fields(cmd);

if isfield(cmd, 'weights') && isfield(cmd, 'snapshot')
    error('Caffe command cannot be with "weights" and "snapshot" simultaneously!');
end

if isfield(cmd, 'weights')
    cmd.weights = srdata.checkFileFormat(cmd.weights, '.caffemodel');
elseif isfield(cmd, 'snapshot')
    cmd.snapshot = srdata.checkFileFormat(cmd.snapshot, '.solverstate');
end

%%
net = mycaffe.net(net, solv, cmd, Train, Test, flag);
solv = mycaffe.solv(net, solv, cmd);

%% generate training cmd and copy it to clipboard
cmd.solver = solv.file;
solver_mode = cmd.solver_mode;
if strcmp(solver_mode(1:2), '--')
	solver_mode = solver_mode(3:end);
end

if isfield(cmd, 'caffe')
	caffe = cmd.caffe;
elseif isfield(cmd, 'caffe_version')
	caffe = mycaffe.caffe_cmd(cmd.caffe_version);
end
cmd = mycaffe.rmFields(cmd, {'solver_mode', 'caffe', 'caffe_version'});

cmdparams = fieldnames(cmd);
for i = 1 : length(cmdparams)
    cmdparams{i} = sprintf('--%s "%s" \\\n', cmdparams{i}, cmd.(cmdparams{i}));
end
cmdparams = [cmdparams{:}];

%%
TrainingCmd = sprintf([
    'GLOG_logtostderr=0 \\\n'...
    'GLOG_log_dir="%s" \\\n'...
    '"%s" train \\\n'...
    '%s--%s'], net.log_path, caffe, cmdparams, solver_mode);

sh_file = fullfile(fileparts(net.log_path), 'training.sh');
mycaffe.net.genFile(sh_file, TrainingCmd);

if nargout > 1
    return
end

% clipboard('copy', TrainingCmd);

fprintf([
    '%% ==============================================================\n'...
    '%% Please use the following shell command to train your network.\n'...
    '%% Note that the shell command has been copied to your clipboard,\n'...
    '%%  and you can directly paste them on your shell window.\n'...
    '%s\n'
    ], TrainingCmd);
% system(CaffeTrainCmd)

